package com.yeyks.appcontroller;

import com.alibaba.fastjson.JSONObject;
import com.yeyks.base.Arguments;
import com.yeyks.exception.ServiceException;
import com.yeyks.getui.service.GeTuiService;
import com.yeyks.model.Ajax.AjaxResponseBody;
import com.yeyks.util.Splitters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fuchangshuai
 * @date 2019/4/22 0022
 */
@ApiIgnore
@Api(tags = "个推接口", description = "个推")
@Slf4j
@RestController
@RequestMapping(value = "/app/getui")
public class GeTuiController {

    @Autowired
    private GeTuiService geTuiService;

    @ApiOperation(value = "给个人或多人推送", notes = "用户id多个之间逗号隔开")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", value = "userId", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "title", value = "标题", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "content", value = "内容", required = true, dataType = "String")
    })
    @PostMapping(value = "/single/or/more")
    public ResponseEntity<?> singleOrMore(@RequestParam(name = "userId") String userId,
                                    @RequestParam(name = "title") String title,
                                    @RequestParam(name = "content") String content) {
        List<String> userIds = getUserIds(userId);
        checkTitleContent(title, content);
        Map<String, String> map = getMap(title, content);
        if (userIds.size() == 1) {
            geTuiService.singlePush(userId, title, JSONObject.toJSONString(map));
        } else {
            geTuiService.morePush(userIds, title, JSONObject.toJSONString(map));
        }
        return ResponseEntity.ok(AjaxResponseBody.success());
    }

    private Map<String, String> getMap(String title, String content) {
        Map<String, String> map = new HashMap<>();
        map.put("title", title);
        map.put("content", content);
        return map;
    }


    @ApiOperation(value = "给所有人推送", notes = "给所有人推送")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "activityId", value = "标题", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "title", value = "标题", required = true, dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "content", value = "内容", required = true, dataType = "String")
    })
    @PostMapping(value = "/all")
    public ResponseEntity<?> pushAll(@RequestParam(name = "title") String title,
                                     @RequestParam(name = "content") String content) {
        checkTitleContent(title, content);
        Map<String, String> map = getMap(title, content);
        geTuiService.allPush(title, JSONObject.toJSONString(map));
        return ResponseEntity.ok(AjaxResponseBody.success());
    }

    private List<String> getUserIds(String userId) {
        List<String> userIds = new ArrayList<>();
        try {
            userIds = Splitters.splitToString(userId, Splitters.COMMA);
        } catch (Exception e) {
            log.error("通过用户id获取集合失败{}", userId);
        }
        if (userIds == null || userIds.size() == 0) {
            throw new ServiceException("个推用户不能为空");
        }
        return userIds;
    }

    private void checkTitleContent(String title, String content) {
        if (!Arguments.notEmpty(title)) {
            throw new ServiceException("个推标题不能为空");
        }
        if (!Arguments.notEmpty(content)) {
            throw new ServiceException("个推内容不能为空");
        }
    }
}
